import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

import Project.Constants;
import Project.SaveFile;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

public class SaveFileTest extends TestCase {
	public static Test suite() {
		return new TestSuite(SaveFileTest.class);
	}

	public void testCreateAFile() {
		int numFiles = SaveFile.getNumberOfFiles();
		// test that numFiles is correct
		File test;
		if (numFiles != 0) {
			Assert.assertTrue(numFiles >= 0);
			test = new File("src/SaveFiles/save" + (numFiles - 1) + ".ser");
			Assert.assertTrue(test.exists() == true);
		}

		test = new File("src/SaveFiles/save" + (numFiles) + ".ser");
		Assert.assertTrue(test.exists() == false);

		// make a new file
		SaveFile.CreateNewFile();
		Assert.assertTrue(SaveFile.getNumberOfFiles() == numFiles + 1);

		test = new File("src/SaveFiles/save" + (numFiles) + ".ser");
		Assert.assertTrue(test.exists() == true);
	}
	
	public void testGetNumberOfFiles(){
		int numFiles = -1;
		BufferedReader in;
		try {
			in = new BufferedReader(new FileReader("src/SaveFiles/info.txt"));

			String str = in.readLine();
			numFiles = Integer.parseInt(str);
			in.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Assert.assertTrue(numFiles == SaveFile.getNumberOfFiles());
	}
	
	public void testFileChanges(){
		if(SaveFile.getNumberOfFiles() == 0){
			return;
		}
		double money = SaveFile.LoadFile(0, true).getMoney();
		SaveFile.getCurrentFile().setMoney(money +100);
		SaveFile.SaveCurrentFile();
		SaveFile.UnLoadCurrentFile();
		Assert.assertTrue(SaveFile.getCurrentFile() == null);
		SaveFile.LoadFile(0, true);
		Assert.assertTrue(SaveFile.getCurrentFile().getMoney() == money + 100);
	}

}
